﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IP.Core.IPData;
using IP.Core.IPUserService;
using WebDS.CDBNames;
using IP.Core.IPCommon;
using WebDS;
using WebUS;
public partial class DanhMuc_F102_Dm_benh_vien : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!this.IsPostBack)
            {
                load_cbo_tinh();
                load_cbo_huyen();                
                load_data_2_grid();
                m_cmd_cap_nhat.Visible = false;
            }

        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }

    #region Members
    US_DM_DIA_PHUONG_BENH_VIEN m_us_dm_benh_vien = new US_DM_DIA_PHUONG_BENH_VIEN();
    DS_DM_DIA_PHUONG_BENH_VIEN m_ds_dm_benh_vien = new DS_DM_DIA_PHUONG_BENH_VIEN();
    #endregion

    #region Data Structures

    #endregion

    #region Public Methods
    public string get_maping_dia_chi(object ip_dc_id)
    {
        US_DM_DIA_PHUONG_BENH_VIEN v_us_huyen = new US_DM_DIA_PHUONG_BENH_VIEN(CIPConvert.ToDecimal(ip_dc_id));
        //  Nếu như đây là loại tỉnh
        if (v_us_huyen.IsID_DIA_PHUONG_CHANull()) return v_us_huyen.strTEN_DIA_PHUONG;
        US_DM_DIA_PHUONG_BENH_VIEN v_us_tinh = new US_DM_DIA_PHUONG_BENH_VIEN(v_us_huyen.dcID_DIA_PHUONG_CHA);
        return v_us_huyen.strTEN_DIA_PHUONG + " - " + v_us_tinh.strTEN_DIA_PHUONG;
    }
    #endregion
    
    #region Private Methods
    private void load_data_2_grid()
    {
        try
        {
            m_us_dm_benh_vien.FillDataset(m_ds_dm_benh_vien, " WHERE "+ DM_DIA_PHUONG_BENH_VIEN.ID_LOAI_DIA_PHUONG +" = 4");
            m_grv_dm_benh_vien.DataSource = m_ds_dm_benh_vien.DM_DIA_PHUONG_BENH_VIEN;
            m_grv_dm_benh_vien.DataBind();
            if (m_ds_dm_benh_vien.DM_DIA_PHUONG_BENH_VIEN.Rows.Count == 0)
            {
                m_lbl_thong_bao.Text = "Chưa có bệnh viện thực huyện này";
                m_lbl_thong_bao.Visible = true;
            }
            else m_lbl_thong_bao.Visible = false;
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void load_cbo_tinh()
    {
        try
        {
            US_DM_DIA_PHUONG_BENH_VIEN v_us_dm_tinh = new US_DM_DIA_PHUONG_BENH_VIEN();
            DS_DM_DIA_PHUONG_BENH_VIEN v_ds_dm_tinh = new DS_DM_DIA_PHUONG_BENH_VIEN();
            v_us_dm_tinh.FillDataset(v_ds_dm_tinh, " WHERE ID_LOAI_DIA_PHUONG = " + (int)e_loai_dia_phuong.TINH);
            m_cbo_tinh.DataSource = v_ds_dm_tinh.DM_DIA_PHUONG_BENH_VIEN;
            m_cbo_tinh.DataTextField = DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG;
            m_cbo_tinh.DataValueField = CM_DM_LOAI_TD.ID;
            m_cbo_tinh.DataBind();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void load_cbo_huyen()
    {
        try
        {
            US_DM_DIA_PHUONG_BENH_VIEN v_us_dm_huyen = new US_DM_DIA_PHUONG_BENH_VIEN();
            DS_DM_DIA_PHUONG_BENH_VIEN v_ds_dm_huyen = new DS_DM_DIA_PHUONG_BENH_VIEN();
            v_us_dm_huyen.Fill_data_by_id_cha(v_ds_dm_huyen, CIPConvert.ToDecimal(m_cbo_tinh.SelectedValue));
            m_cbo_huyen.DataSource = v_ds_dm_huyen.DM_DIA_PHUONG_BENH_VIEN;
            m_cbo_huyen.DataTextField = DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG;
            m_cbo_huyen.DataValueField = CM_DM_LOAI_TD.ID;
            m_cbo_huyen.DataBind();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void delete_dm_benh_vien(int i_int_row_index)
    {
        try
        {
            decimal v_dc_id_dm_tu_dien = CIPConvert.ToDecimal(m_grv_dm_benh_vien.DataKeys[i_int_row_index].Value);
            m_us_dm_benh_vien.DeleteByID(v_dc_id_dm_tu_dien);
            load_data_2_grid();
            m_lbl_mess.Text = "Xóa bản ghi thành công.";
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình xóa bản ghi.";
            throw v_e;
        }
    }
    private void load_update_dm_benh_vien(int i_int_row_index)
    {
        try
        {
            decimal v_dc_id_dm_benh_vien = CIPConvert.ToDecimal(m_grv_dm_benh_vien.DataKeys[i_int_row_index].Value);
            US_DM_DIA_PHUONG_BENH_VIEN v_us_dm_benh_vien = new US_DM_DIA_PHUONG_BENH_VIEN(v_dc_id_dm_benh_vien);
            m_hdf_id_dm_tu_dien.Value = CIPConvert.ToStr(v_dc_id_dm_benh_vien);
            us_object_2_form(v_us_dm_benh_vien);
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void clear_dm_tu_dien()
    {
        try
        {
            m_grv_dm_benh_vien.EditIndex = -1;
            m_txt_thong_tin.Text = "";
            m_txt_ten_bv.Text = "";            
            this.m_hdf_id_dm_tu_dien.Value = "";
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private bool check_validate()
    {
        if (this.m_cbo_huyen.SelectedItem == null)
        {
            this.m_ctv_huyen.IsValid = false;
            return false;
        }
        if (this.m_txt_ten_bv.Text.Trim().Equals(""))
        {
            this.m_cvt_ten_bv.IsValid = false;
            return false;
        }        

        return true;
    }    
    private void insert_dm_tu_dien()
    {
        try
        {
            m_grv_dm_benh_vien.EditIndex = -1;
            if (Page.IsValid)
            {
                if (!check_validate()) return;                
                form_2_us_object();
                m_us_dm_benh_vien.Insert();
                m_lbl_mess.Text = "Đã thêm mới thành công.";
                clear_dm_tu_dien();
                load_data_2_grid();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình thêm mới bản ghi.";
            throw v_e;
        }
    }
    private void update_dm_tu_dien()
    {
        try
        {
            m_grv_dm_benh_vien.EditIndex = -1;
            if (Page.IsValid)
            {
                if (this.m_hdf_id_dm_tu_dien.Value == "") { m_lbl_mess.Text = "Bạn phải chọn Từ điển cần Cập nhật."; return; }
                if (!check_validate()) return;
                form_2_us_object();
                m_us_dm_benh_vien.dcID = CIPConvert.ToDecimal(this.m_hdf_id_dm_tu_dien.Value);
                m_us_dm_benh_vien.Update();
                m_lbl_mess.Text = "Đã cập nhật bản ghi thành công.";
                clear_dm_tu_dien();
                m_grv_dm_benh_vien.EditIndex = -1;
                load_data_2_grid();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình cập nhật bản ghi.";
            throw v_e;
        }
    }
    private void us_object_2_form(US_DM_DIA_PHUONG_BENH_VIEN i_us)
    {
        US_DM_DIA_PHUONG_BENH_VIEN v_us = new US_DM_DIA_PHUONG_BENH_VIEN(i_us.dcID_DIA_PHUONG_CHA);
        m_cbo_tinh.SelectedValue = CIPConvert.ToStr(v_us.dcID_DIA_PHUONG_CHA);
        load_cbo_huyen();
        m_cbo_huyen.SelectedValue = CIPConvert.ToStr(i_us.dcID_DIA_PHUONG_CHA);
        m_txt_ten_bv.Text = i_us.strTEN_DIA_PHUONG;
        m_txt_thong_tin.Text = i_us.strGHI_CHU;        
    }
    private void form_2_us_object()
    {
        m_us_dm_benh_vien.strTEN_DIA_PHUONG = m_txt_ten_bv.Text;
        m_us_dm_benh_vien.dcID_LOAI_DIA_PHUONG =  (decimal)e_loai_dia_phuong.BENH_VIEN;
        m_us_dm_benh_vien.dcID_DIA_PHUONG_CHA =  CIPConvert.ToDecimal( m_cbo_huyen.SelectedValue);
        m_us_dm_benh_vien.strGHI_CHU = m_txt_thong_tin.Text;
    }    
    #endregion

    #region Events
    protected void m_cbo_tinh_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            load_cbo_huyen();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cbo_huyen_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            load_data_2_grid();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_grv_dm_benh_vien_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            delete_dm_benh_vien(e.RowIndex);
        }
        catch (Exception v_e)
        {
            // de su dung CsystemLog_301 bat buoc Site phai dat trong thu muc cap 1. Vi du: DanhMuc/Dictionary.aspx
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_tao_moi_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            insert_dm_tu_dien();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            clear_dm_tu_dien();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            m_cmd_tao_moi.Visible = true;
            m_cmd_cap_nhat.Visible = false;
            update_dm_tu_dien();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_grv_dm_benh_vien_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            m_cmd_tao_moi.Visible = false;
            m_cmd_cap_nhat.Visible = true;
            load_update_dm_benh_vien(e.RowIndex);
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    #endregion
}